﻿
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using fc2editor.DataModel;
using fc2editor.DataModel.Descriptors;
namespace fc2editor.Lua
{
    public class WeaponParser
    {
        //^weapons_category\("{(.+)}"\s*,\s*"(.+)"\s*,\s*
        private static readonly Regex WeaponCategoryRegex = new Regex("^weapons_category\\(\"{(.+)}\"\\s*,\\s*\"(.+)\"\\s*,\\s*");

        //^launcher\("{(.+)}"\s*,\s*"(.+)"\s*,\s*
        private static readonly Regex LauncherRegex = new Regex("^launcher\\(\"{(.+)}\"\\s*,\\s*\"(.+)\"\\s*,\\s*");

        //^(\w+) = "(.+)",
        private static readonly Regex LauncherPropertyRegex = new Regex("^(\\w+) = \"(.+)\",");

        public Weapon[] Parse(string fileName)
        {
            var result = new List<Weapon>();

            using (var fs = File.OpenRead(fileName))
            {
                using (var reader = new StreamReader(fs))
                {
                    while (!reader.EndOfStream)
                    {
                        var line = reader.ReadLine().Trim();

                        if (!line.StartsWith("weapons_category"))
                        {
                            if (parsingCategory)
                            {
                                if (parsingLauncher)
                                {
                                    var m = LauncherPropertyRegex.Match(line);
                                    if (m.Success)
                                    {
                                        switch (m.Groups[1].Value.ToLower())
                                        {
                                            case "picture":
                                                var rawName = m.Groups[2].Value;
                                                currentDescriptor.Picture = rawName.ToLower(); // hack due-to image names
                                                break;
                                            case "guided":
                                                if (m.Groups[2].Value.ToLower() == "yes")
                                                    currentDescriptor.IsGuided = true;
                                                break;
                                        }
                                    }

                                    if (line == "{")
                                        continue;
                                    if (line == "},")
                                    {
                                        var newWeapon = new Weapon(currentDescriptor);
                                        result.Add(newWeapon);
                                        parsingLauncher = false;
                                    }
                                }
                                else
                                {
                                    if (!line.StartsWith("launcher"))
                                        continue;
                                    else
                                    {
                                        var m = LauncherRegex.Match(line);
                                        if (m.Success)
                                        {
                                            parsingLauncher = true;
                                            currentDescriptor = new WeaponDescriptor();
                                            currentDescriptor.ID = Guid.Parse(m.Groups[1].Value);
                                            currentDescriptor.Name = m.Groups[2].Value;
                                            currentDescriptor.Category = currentCategory;
                                        }
                                    }
                                }
                            }
                            else
                            {

                            }
                        }
                        else
                        {
                            var m = WeaponCategoryRegex.Match(line);
                            if (m.Success)
                            {
                                parsingCategory = true;

                                Guid id = Guid.Parse(m.Groups[1].Value);
                                string name = m.Groups[2].Value;
                                currentCategory = new WeaponsCategory(id, name);
                            }
                        }
                    }
                }
            }

            return result.ToArray();
        }

        private WeaponsCategory currentCategory;
        private bool parsingCategory;
        private bool parsingLauncher;

        private WeaponDescriptor currentDescriptor;
    }
}
